package com.xmut.service.impl;

import com.xmut.mapper.OldManMapper;
import com.xmut.pojo.ManagerSet;
import com.xmut.pojo.OldMan;
import com.xmut.service.OldManService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;

@Service
public class OldManServiceImpl implements OldManService {

    @Autowired
    private OldManMapper oldManMapper;
    @Override
    public List<OldMan> getList() {

        return oldManMapper.getList();

    }
    @Override
    public void oldManDel(Integer id) {
        oldManMapper.oldManDel(id);
    }

    @Override
    public void oldManIns(OldMan oldMan) {
        //时间的处理
        DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime localDateTime = LocalDateTime.now();
        String stringTime = fmt.format(localDateTime);

        //性别的处理
        String sex = "";
        if (oldMan.getGender().equals("男"))
        {
            sex = "1";
        }
        else
        {
            sex = "2";
        }

        oldMan.setEntrydate(stringTime);
        oldMan.setGender(sex);
        oldManMapper.oldManIns(oldMan);
    }

    @Override
    public void oldManUpd(Integer id, OldMan oldMan) {

        String sex = "";
        if (oldMan.getGender().equals("男"))
        {
            sex = "1";
        }
        else
        {
            sex = "2";
        }
        oldMan.setGender(sex);

        String oldManState = "正常";
        System.out.println(oldMan.getTemperature());
        double temperature = Double.parseDouble(oldMan.getTemperature());
        double blood = Double.parseDouble(oldMan.getBloodpressure());
        double heart = Double.parseDouble(oldMan.getHeartrate());


        ManagerSet managerSet = oldManMapper.getManagerSet(Integer.valueOf(oldMan.getManagerid()));


        if(temperature<managerSet.getTemperdown() || temperature>managerSet.getTemperup() ||
                blood > managerSet.getBloodup() || blood< managerSet.getBlooddown() ||
                heart < managerSet.getHeartdown() ||heart > managerSet.getHeartup())
        {
            oldManState = "异常";
        }

        String gender = oldMan.getGender();
        Integer age = oldMan.getAge();
        String TotalCho = oldMan.getTotalCho();
        String HDLCho = oldMan.getHDLCho();
        String Smoking = oldMan.getSmoking();
        int smokingState = Smoking.equals("0")?0:1;
        double R = 0.0;
        if(sex.equals("1"))
        {
            //x = -6.594 + 0.0443 \times \log(\text{年龄}) + 0.0118 \times \log(\text{总胆固醇})
            // - 0.0087 \times \log(\text{高密度脂蛋白胆固醇}) + 0.7095 \times \text{吸烟情况} + 0.0181 \times \text{血压}
            //风险系数得分的计算
            double x = -6.594 + 0.0443 * Math.log(age) + 0.0118 * Math.log(Double.parseDouble(TotalCho)) - 0.0087
                    * Math.log(Double.parseDouble(HDLCho)) + 0.7095 * smokingState + 0.0181 * blood;
            //心血管疾病风险概率 [R = (1 - 0.987^{e^x}) \times 100]
           R = ((1 - Math.pow(0.987,Math.exp(x))) * 100)*100;

        }
        else
        {
            //x = -6.594 + 0.0864 \times \log(\text{年龄}) + 0.0455 \times \log(\text{总胆固醇})
            // -0.0181 \times \log(\text{高密度脂蛋白胆固醇}) + 0.9202 \times \text{吸烟情况} + 0.0209 \times \text{血压}

            double x = -6.594 + 0.0864 * Math.log(age) + 0.0455 * Math.log(Double.parseDouble(TotalCho)) - 0.0181
                    * Math.log(Double.parseDouble(HDLCho)) +  0.9202 * smokingState + 0.0209 * blood;
            R = ((1 - Math.pow(0.987,Math.exp(x))) * 100)*100;

        }

        oldMan.setDisease(String.format("%.2f",R));
        oldMan.setId(id);
        oldMan.setState(oldManState);
        oldManMapper.oldManUpd(oldMan);
    }

    @Override
    public OldMan getById(Integer id) {
        return oldManMapper.getById(id);
    }

    @Override
    public List<OldMan> getByManagerId(Integer id) {
        return oldManMapper.getByManagerId(id);
    }

    @Override
    public ManagerSet getSet(Integer id) {
        return oldManMapper.getManagerSet(id);
    }

    @Override
    public void setManaSet(Integer id, ManagerSet managerSet) {
        managerSet.setManagerid(id);
        oldManMapper.setManaSet(managerSet);
    }

    @Override
    public void setEvaluate(Integer id, Integer score) {
        String evaluate = "暂未测评";
        if(score>=0&&score<=29)
        {
            evaluate = "完全失能";
        }
        else if(score>=30&&score<=45)
        {
            evaluate = "重度失能";
        }
        else if(score>=46&&score<=65)
        {
            evaluate = "中度失能";
        }
        else if(score>=66&&score<=89)
        {
            evaluate = "轻度失能";
        }
        else if(score>90&&score<=95)
        {
            evaluate = "能力完好";
        }

        oldManMapper.setEvaluate(id,evaluate);
    }
}
